/********************************************************************************
 * <PRE> * 파일 정보 *
 * 1. FileName	: FileDownloadUtil.java
 * 2. Package	: com.ncomz.nzeip.common.util
 * 3. 설명        	: TODO
 * 4. 작성자		: P044826
 * 5. 작성일		: 2014. 1. 15. 오후 8:22:59
 * 6. 설명		:
 * </PRE>
 ********************************************************************************/


package com.sunny.hp.common.util;

import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.zip.ZipEntry;
import java.util.zip.ZipOutputStream;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

import com.sunny.hp.common.servlet.DownloadView;
import com.sunny.hp.common.servlet.ModelAndNCOMZView;

/********************************************************************************
 * <PRE> * 프로그램 정보 *
 * 1. Project		: BEMS2.1
 * 2. Package	: com.ncomz.nzeip.common.util
 * 3. FileName	: FileDownloadUtil.java
 * 4. 작성자		: P044826
 * 5. 작성일		: 2014. 1. 15. 오후 8:22:59
 * 6. 설명			:
 * </PRE>
 ********************************************************************************/
@Controller
@RequestMapping("/fd")
public class FileDownloadUtil {
	

    /**********************************************************************
     * <PRE> * 북마크 등록 화면 연결 *
     * 1. MethodName	: registBookMarkForm
     * 2. ClassName		: BookMarkMagementController
     * 3. 작성자			: P044826
     * 4. 작성일			: 2013. 11. 26. 오후 6:35:38
     * 5. 설명				:
     * </PRE>
     * 		@return ModelAndNCOMZView
     * 		@param request
     * 		@param response
     * 		@return
     * 		@throws Exception
    **********************************************************************/
    @RequestMapping("/ajax/download.do")
    public ModelAndView download(HttpServletRequest request, HttpServletResponse response) throws Exception {
    	HttpRequest requestUtil = new HttpRequest(request); 
		LinkedHashMap param = requestUtil.paramBind();
		
    	File downLoadFile = getFile(request);
    	String FILE_NAME = param.get("FILE_NAME")+"";
		HashMap model = new HashMap();
		model.put("downloadFile", downLoadFile);
		model.put("downloadFileName", FILE_NAME);
		return new ModelAndNCOMZView(request, new DownloadView(), model);
    }
    
    public File getFile(HttpServletRequest request){

		//ModelAndNCOMZView mav = new ModelAndNCOMZView(request);
		HttpRequest requestUtil = new HttpRequest(request); 
		LinkedHashMap param = requestUtil.paramBind();
		
		String FILE_PATH = param.get("FILE_PATH")+"";
		
		String image_server_path = PropertiesUtil.getMessage("server.image-server");
		String image_root_path = PropertiesUtil.getMessage("fileattach.realUploadPath");
		String webuploadpath = PropertiesUtil.getMessage("fileattach.webview.path");
		FILE_PATH = FILE_PATH.replaceAll(image_server_path+ webuploadpath, image_root_path);
		
		return new File(FILE_PATH);
    }
    
    /**********************************************************************
     * <PRE> * 메소드 정보 *
     * 1. MethodName	: selectReportTemplateManagementDown
     * 2. ClassName		: FileDownloadUtil
     * 3. 작성자			: P044828
     * 4. 작성일			: 2014. 2. 27. 오후 4:32:33
     * 5. 설명				:
     * </PRE>
     * 		@return ModelAndView
     * 		@param request
     * 		@param response
     * 		@param session
     * 		@throws Exception
    **********************************************************************/
    @RequestMapping("/ajax/selectFileTemplateDownload.do")
	public ModelAndNCOMZView selectReportTemplateManagementDown(HttpServletRequest request, HttpServletResponse response, HttpSession session)  throws Exception {
		HttpRequest requestUtil = new HttpRequest(request); 
		LinkedHashMap param = requestUtil.paramBind();
		
		File downLoadFile = null;
		
		String menu_nm  = param.get("menu_nm") == null ? "" : param.get("menu_nm").toString();
		String file_nm  	  = param.get("fileName")  == null ? "" : param.get("fileName").toString();
		String reFile_nm   = param.get("reFile_nm") == null ? file_nm : param.get("reFile_nm").toString();
		
		if(menu_nm.equals("BAS_DATA")){
			downLoadFile = new File(PropertiesUtil.getMessage("basdataTemplate.downloadPath") + "/"  + file_nm);
		}else if(menu_nm.equals("FILE_UPLOAD")){
			downLoadFile = new File(PropertiesUtil.getMessage("facilityUpload.downloadPath") + "/"  + file_nm);
			
		}


		HashMap model = new HashMap();
		model.put("downloadFile", downLoadFile);
		model.put("downloadFileName", reFile_nm);
		return new ModelAndNCOMZView(request, new DownloadView(), model);
	}
    
    public void zipFiles(List<String> files){
        
        FileOutputStream fos = null;
        ZipOutputStream zipOut = null;
        FileInputStream fis = null;
        try {
            fos = new FileOutputStream("C:/testing.zip");
            zipOut = new ZipOutputStream(new BufferedOutputStream(fos));
            for(String filePath:files){
                File input = new File(filePath);
                fis = new FileInputStream(input);
                ZipEntry ze = new ZipEntry(input.getName());
                System.out.println("Zipping the file: "+input.getName());
                zipOut.putNextEntry(ze);
                byte[] tmp = new byte[4*1024];
                int size = 0;
                while((size = fis.read(tmp)) != -1){
                    zipOut.write(tmp, 0, size);
                }
                zipOut.flush();
                fis.close();
            }
            zipOut.close();
            System.out.println("Done... Zipped the files...");
        } catch (FileNotFoundException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } finally{
            try{
                if(fos != null) fos.close();
            } catch(Exception ex){
                 
            }
        }
    }
 
}
